package com.github.browep.bat;

import android.content.Context;
import android.util.Log;
import com.github.browep.bat.activities.*;
import com.google.common.collect.ImmutableMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.github.browep.bat.util.XmlUtil.getChildElementListWithTag;
import static com.github.browep.bat.util.XmlUtil.getElementByTagName;

/**
 * Created by IntelliJ IDEA.
 * User: paul
 * Date: 8/26/11
 * Time: 4:01 PM
 * To change this template use File | Settings | File Templates.
 */
public class ScreenService {
    public static final String LOG_TAG = ScreenService.class.getSimpleName();

    Context context;
    int resourceId;
    Element rootElement;
    Map<String, Screen> screens = new HashMap<String, Screen>();

    public ScreenService(Context context, String assetFileName) {
        this.context = context;
        try {

            InputStream inputStream = context.getAssets().open(assetFileName);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = null;
            db = dbf.newDocumentBuilder();
            Document doc = db.parse(inputStream);
            rootElement = doc.getDocumentElement();
            Element screensElement = getElementByTagName(rootElement, "screens");
            List<Element> screenElems = getChildElementListWithTag(screensElement,"screen");
            for(Element screenElement : screenElems)
                screens.put(screenElement.getAttribute("id"),new Screen(screenElement));

        } catch (Exception e) {
            Log.e(LOG_TAG, "error parsing: " + resourceId, e);
        }
    }

    public Screen getScreenById(String id) {
        return screens.get(id);
    }

    public static class Screen {

        public static enum Type { TILES, HTML, LOCATIONS, MENU, PUNCH_CARD }

        private static Map<String,Type> typeToEnum = ImmutableMap.of(
                "tiles",Type.TILES,
                "html",Type.HTML,
                "locations",Type.LOCATIONS,
                "menu",Type.MENU,
                "punch-card",Type.PUNCH_CARD
        );

        private static ImmutableMap<Type, Class<? extends BaseActivity>> typeToActivityClass = ImmutableMap.of(
                Type.TILES, Home.class,
                Type.HTML, Html.class,
                Type.LOCATIONS, Locations.class,
                Type.MENU, Menu.class,
                Type.PUNCH_CARD, PunchCardActivity.class
        );

        private  Type type;

        private Element screenElement;

        public Screen(Element element){
            this.screenElement = element;
        }

        public String getName(){
            return  getElementByTagName(screenElement,"name").getTextContent();
        }

        public Type getType() {
            if(type == null){
                type = typeToEnum.get(screenElement.getAttribute("type"));
            }
            return type;
        }

        public Element getScreenElement() {
            return screenElement;
        }

        public Class getActivityClass(){
            return typeToActivityClass.get(getType());
        }

    }


}
